package com.zhaosc.parse;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ReferenceBeanDefinitionParser implements BeanDefinitionParser {
	/*
	 * <xsd:complexType> <xsd:attribute name="id"
	 * type="xsd:string"></xsd:attribute> <xsd:attribute name="interface"
	 * type="xsd:string"></xsd:attribute> <xsd:attribute name="loadbalance"
	 * type="xsd:string"></xsd:attribute> <xsd:attribute name="protocol"
	 * type="xsd:string"></xsd:attribute> </xsd:complexType>
	 */
	private Class<?> beanClass;

	public ReferenceBeanDefinitionParser(Class<?> beanClass) {
		this.beanClass = beanClass;
	}

	@Override
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		String id=element.getAttribute("id");
		String interf=element.getAttribute("interface");
		String loadbalance=element.getAttribute("loadbalance");
		String protocol=element.getAttribute("protocol");
	
        if (id == null || "".equals(id)) {
            throw new RuntimeException("Reference id 不能为空！");
        }
        if (interf == null || "".equals(interf)) {
            throw new RuntimeException("Reference interf 不能为空！");
        }
        if (loadbalance == null || "".equals(loadbalance)) {
            throw new RuntimeException("Reference loadbalance 不能为空！");
        }
        if (protocol == null || "".equals(protocol)) {
            throw new RuntimeException("Reference protocol 不能为空！");
        }
        
        RootBeanDefinition beanDefinition=new RootBeanDefinition();
        beanDefinition.getPropertyValues().add("id", id);
        beanDefinition.getPropertyValues().add("interf", interf);
        beanDefinition.getPropertyValues().add("loadbalance", loadbalance);
        beanDefinition.getPropertyValues().add("protocol", protocol);
        
        parserContext.getRegistry().registerBeanDefinition("Reference" + id,
                beanDefinition);
        
		return beanDefinition;
	}

}
